package com.stone.framework.push.service;

import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stone.framework.message.push.MessageReqBody;
import com.stone.framework.push.websocket.WebSocketEndpoint;

@Service
public class MessageService {

    @Autowired
    private WebSocketEndpoint webSocketEndpoint;

    public void push(MessageReqBody reqBody) {
        this.pushPc(reqBody);
        this.pushApp(reqBody);
        this.pushSms(reqBody);
        this.pushWechat(reqBody);
    }

    public void pushPc(MessageReqBody reqBody) {
        this.doPush(reqBody, body -> webSocketEndpoint.sendAll(body), body -> webSocketEndpoint.send(body));
    }

    public void pushApp(MessageReqBody reqBody) {

    }

    public void pushSms(MessageReqBody reqBody) {

    }

    public void pushWechat(MessageReqBody reqBody) {

    }

    private void doPush(MessageReqBody reqBody, Consumer<MessageReqBody> positive, Consumer<MessageReqBody> negative) {
        if (reqBody.isAll()) {
            positive.accept(reqBody);
        } else {
            negative.accept(reqBody);
        }
    }

}
